package cn.edu.scau.cmi.ema.service;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Crawlerdetect;
import cn.edu.scau.cmi.ema.domain.Crawlerdetectstatus;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;
@Service("CrawlerdetectService")
@Transactional

public class CrawlerdetectServiceImpl extends CmiEmaService implements CrawlerdetectService {
	public CrawlerdetectServiceImpl() {
	}

	@Transactional
	public Crawlerdetect saveCrawlerdetect(Crawlerdetect crawlerdetect) {

		if (crawlerdetect.getId() == null) {
			Crawlerdetectstatus crawlerdetectstatus = crawlerdetectstatusDAO
					.findCrawlerdetectstatusById(crawlerdetect.getCrawlerdetectstatus().getId());

			Crawlerdetect existingCrawlerdetect = crawlerdetectDAO
					.findCrawlerdetectByPhysicalPrimaryKey(crawlerdetect.getFilename(), crawlerdetect.getRownumber());

			crawlerdetect.setCrawlerdetectstatus(crawlerdetectstatus);

			if (isEquals(crawlerdetect, existingCrawlerdetect)) {
				return existingCrawlerdetect;
			}

			crawlerdetect = crawlerdetectDAO.store(crawlerdetect);
			crawlerdetectDAO.flush();

		} else {
			Crawlerdetect existingCrawlerdetect = crawlerdetectDAO.findCrawlerdetectByPrimaryKey(crawlerdetect.getId());

			crawlerdetect = crawlerdetectDAO.store(crawlerdetect);
			crawlerdetectDAO.flush();
		}

		return crawlerdetect;
	}

	public void deleteCrawlerdetect(Crawlerdetect crawlerdetect) {
		crawlerdetectDAO.remove(crawlerdetect);
		crawlerdetectDAO.flush();
	}

	@Transactional
	public void deleteCrawlerdetects(Set<Integer> deleteCrawlerdetectsId) {
		Query query = crawlerdetectDAO.createNamedQuery("deleteCrawlerdetectsById", -1, -1);
		query.setParameter(1, deleteCrawlerdetectsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Crawlerdetect> loadCrawlerdetects() {
		return crawlerdetectDAO.findAllCrawlerdetects();

	}

	@Transactional
	public Set<Crawlerdetect> loadReferenceCrawlerdetects(Set<Crawlerdetect> crawlerdetects) {
		Iterator<Crawlerdetect> crawlerdetectsIterator = crawlerdetects.iterator();
		Set<Crawlerdetect> referencedCrawlerdetects = new HashSet<>();
		while (crawlerdetectsIterator.hasNext()) {
			Crawlerdetect currentCrawlerdetect = crawlerdetectsIterator.next();

		}
		return referencedCrawlerdetects;
	}

	public Set<Crawlerdetect> loadCrawlerdetectsByIdSet(Set<Integer> crawlerdetectIdSet) {

		Iterator<Integer> crawlerdetectIdIterator = crawlerdetectIdSet.iterator();
		Set<Crawlerdetect> crawlerdetectSet = new HashSet<>();
		while (crawlerdetectIdIterator.hasNext()) {
			Integer id = crawlerdetectIdIterator.next();
			Crawlerdetect currentCrawlerdetect = crawlerdetectDAO.findCrawlerdetectById(id);
			crawlerdetectSet.add(currentCrawlerdetect);
		}
		return crawlerdetectSet;
	}

	@Transactional
	public List<Crawlerdetect> findAllCrawlerdetects(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Crawlerdetect>(crawlerdetectDAO.findAllCrawlerdetects(startResult, maxRows));
	}

	@Transactional
	public Crawlerdetect findCrawlerdetectByPrimaryKey(Integer id) {
		return crawlerdetectDAO.findCrawlerdetectByPrimaryKey(id);
	}

	@Transactional
	public Integer countCrawlerdetects() {
		return ((Long) crawlerdetectDAO.createQuerySingleResult("select count(o) from Crawlerdetect o")
				.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerdetects(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Crawlerdetect.class);
		return ((Long) crawlerdetectDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 *
	 */
	@Transactional
	public Integer countCrawlerdetectsByName(String queryName) {
		Query query = crawlerdetectDAO.createNamedQuery("countCrawlerdetectsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerdetectsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerdetect.class);
		Session session = (Session) entityManager.getDelegate();
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadCrawlerdetectObjectReferencedSet(Crawlerdetect crawlerdetect) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();
		if (crawlerdetect.getCrawlerdetectstatus() != null) {

			Set<Integer> crawlerdetectstatusIds = new HashSet<Integer>();
			crawlerdetectstatusIds.add(crawlerdetect.getCrawlerdetectstatus().getId());
			objectReferencedSetMap.put("crawlerdetectstatusIds", crawlerdetectstatusIds);
		}

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadCrawlerdetectClassReferencedSet(Crawlerdetect crawlerdetect) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("crawlerdetectstatuss", crawlerdetectstatusDAO.findAllCrawlerdetectstatuss());

		return classReferencedSetMap;
	}

	public boolean isEquals(Crawlerdetect crawlerdetect, Object obj) {
		if (obj == crawlerdetect)
			return true;
		if (!(obj instanceof Crawlerdetect))
			return false;
		Crawlerdetect comparedCrawlerdetect = (Crawlerdetect) obj;
		if (!CheckPropertyEqual(crawlerdetect.getFilename(), comparedCrawlerdetect.getFilename())) {
			return false;
		}
		if (!CheckPropertyEqual(crawlerdetect.getRownumber(), comparedCrawlerdetect.getRownumber())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

